package com.smart.village.big.screen.utils;

import com.alibaba.fastjson.JSON;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

public  class BeginEndDateUtils {
    public static List<BeginEndDate> getYearlyQuarterlyDates(int year) {
        List<BeginEndDate> quarterlyDates = new ArrayList<>();

        // 获取指定年份的第一天和最后一天
        Year y = Year.of(year);

        // 计算每个季度的开始和结束日期
        for (int i = 1; i <= 4; i++) {
            LocalDate startDate = y.atMonth((i - 1) * 3 + 1).atDay(1);
            LocalDate endDate = y.atMonth(i * 3).atEndOfMonth();

            quarterlyDates.add(new BeginEndDate(startDate, endDate));
        }

        return quarterlyDates;
    }

    public static List<BeginEndDate> getYearlyMonthlyDates(int year) {
        List<BeginEndDate> monthlyDates = new ArrayList<>();

        // 获取指定年份的第一天和最后一天
        Year y = Year.of(year);

        // 计算每个月的开始和结束日期
        for (int month = 1; month <= 12; month++) {
            YearMonth yearMonth = YearMonth.of(year, month);
            LocalDate startDate = yearMonth.atDay(1);
            LocalDate endDate = yearMonth.atEndOfMonth();

            monthlyDates.add(new BeginEndDate(startDate, endDate));
        }

        return monthlyDates;
    }

    public static List<BeginEndDate> getYearlyWeeklyDates(int year) {
        List<BeginEndDate> weeklyDates = new ArrayList<>();

        // 获取指定年份的第一天和最后一天
        Year y = Year.of(year);
        LocalDate firstDayOfYear = y.atDay(1);
        LocalDate lastDayOfYear = y.atDay(y.length());

        // 找到该年第一个周一的日期
        LocalDate firstMonday = firstDayOfYear.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));

        // 如果1月1号不是周一，那么第一个周的开始日期会是上一年的某一天
        if (firstDayOfYear.getDayOfWeek() != DayOfWeek.MONDAY) {
            firstMonday = firstDayOfYear.minusDays((firstDayOfYear.getDayOfWeek().getValue() - 1) % 7);
        }

        // 从第一个周一开始，计算每周的开始和结束日期
        LocalDate currentStart = firstMonday;
        while (currentStart.isBefore(lastDayOfYear) || currentStart.isEqual(lastDayOfYear)) {
            LocalDate currentEnd = currentStart.plusDays(6); // 周日
            if (currentEnd.isAfter(lastDayOfYear)) { // 如果超出当年范围，调整为最后一天
                currentEnd = lastDayOfYear;
            }

            weeklyDates.add(new BeginEndDate(currentStart, currentEnd));

            // 移动到下一周
            currentStart = currentStart.plusWeeks(1);
        }

        // 如果最后一个周的结束日期不是周日，那么需要添加一个额外的周，包含12月末的和下一年的月初
        if (lastDayOfYear.getDayOfWeek() != DayOfWeek.SUNDAY) {
            LocalDate nextYearFirstDay = lastDayOfYear.plusDays(1);
            LocalDate nextYearFirstMonday = nextYearFirstDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
            LocalDate nextYearFirstSunday = nextYearFirstDay.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));

            weeklyDates.add(new BeginEndDate(lastDayOfYear, nextYearFirstSunday));
        }

        return weeklyDates;
    }


    static class BeginEndDate{
        LocalDate begin;
        LocalDate end;
        BeginEndDate(LocalDate begin, LocalDate end) {
            this.begin = begin;
            this.end = end;
        }

        public LocalDate getBegin() {
            return begin;
        }

        public void setBegin(LocalDate begin) {
            this.begin = begin;
        }

        public LocalDate getEnd() {
            return end;
        }

        public void setEnd(LocalDate end) {
            this.end = end;
        }

        @Override
        public String toString() {
            return "BeginEndDate{" +
                    "begin=" + begin +
                    ", end=" + end +
                    '}';
        }
    }

    public static void main(String[] args) {
//        List<BeginEndDate> yearlyQuarterlyDates = BeginEndDateUtils.getYearlyQuarterlyDates(2025);
//        List<BeginEndDate> yearlyMonthlyDates = BeginEndDateUtils.getYearlyMonthlyDates(2025);
        List<BeginEndDate> yearlyWeeklyDates = BeginEndDateUtils.getYearlyWeeklyDates(2025);
//        System.out.println(JSON.toJSONString(yearlyQuarterlyDates));
//        System.out.println(JSON.toJSONString(yearlyMonthlyDates));
        System.out.println(JSON.toJSONString(yearlyWeeklyDates));
    }
}
